package Demo3;

import java.util.Arrays;
import java.util.Comparator;

class Students implements Comparable<Students>{
    public String name;
    public int age;

    public Students(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Students o) {
        System.out.println("=============");
        return this.age - o.age;
    }
}
/*class Students {
    public String name;
    public int age;

    public Students(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}*/
class AgeComparator implements Comparator<Students> {
    @Override
    public int compare(Students o1, Students o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Students> {
    @Override
    public int compare(Students o1, Students o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
  /*    Students[] students = new Students[3];
        students[0] = new Students("张三",10);
        students[1] = new Students("李四",4);
        students[2] = new Students("王五",8);
      System.out.println("排序前"+ Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后"+Arrays.toString(students));*/
        Students students1 = new Students("z张三",10);
        Students students2 = new Students("l李四",7);
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(students1, students2));
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(students1, students2));

    }
}
